package com.example.foodisimportant.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.foodisimportant.Bean.SellerEntity;
import com.example.foodisimportant.R;

import java.util.ArrayList;

public class SellerListAdapter extends RecyclerView.Adapter<SellerListAdapter.myViewHolder> {

    private Context context;
    private ArrayList<SellerEntity> sellerEntityList;

    //创建构造函数
    public SellerListAdapter(Context context, ArrayList<SellerEntity> sellerEntityList){
        //将传递过来的数据赋值给本地变量
        this.context=context;//上下文
        this.sellerEntityList = sellerEntityList;//实体类数据ArrayList
    }

    /**
     * 创建viewholder，相当于listview中的getview中创建view和viewholder
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public SellerListAdapter.myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       //创建自定义布局
        View itemView=View.inflate(context, R.layout.item1,null);
        return new myViewHolder(itemView);
    }

    /**
     * 绑定数据，数据与view绑定
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull SellerListAdapter.myViewHolder holder, int position) {
        //根据点击位置绑定数据
        SellerEntity data= sellerEntityList.get(position);
        holder.mItemSellerName.setText(data.sellerName);
        holder.mItemSellerPos.setText(data.sellerPos);
        holder.mItemSellerImg.setImageResource(R.mipmap.a);
    }

    @Override
    public int getItemCount() {
        return sellerEntityList.size();
    }

    public class myViewHolder extends RecyclerView.ViewHolder {
        private ImageView mItemSellerImg;
        private TextView mItemSellerName;
        private TextView mItemSellerPos;

        public myViewHolder(View itemView) {
            super(itemView);
            mItemSellerImg=itemView.findViewById(R.id.iv_sv);
            mItemSellerName=itemView.findViewById(R.id.tv_name_of_sell);
            mItemSellerPos=itemView.findViewById(R.id.tv_pos_of_sell);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener!=null){
                        onItemClickListener.OnItemClick(v, sellerEntityList.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    /**
     * 设置item的监听事件
     */
    public interface OnItemClickListener{
        /**
         * 接口中的点击每一项的实现方法，参数自己定义
         */
        public void OnItemClick(View view, SellerEntity data);
    }

    //需要外部访问，所以需要设置set方法，方便调用
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
}
